package com.hemaapp.yjnh.activity;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.adapter.RebateAdapter;
import com.hemaapp.yjnh.bean.Rebate;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.yjnh.view.HeaderFooterRecycler.HeaderFooterRecyclerView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomRefreshLoadmoreLayout;

/**
 * 每日收益明细
 */
public class EarningDetailsActivity extends BaseActivity {

    @Bind(R.id.recycler_view)
    HeaderFooterRecyclerView recyclerView;
    @Bind(R.id.refreshLoadmoreLayout)
    RefreshLoadmoreLayout refreshLoadmoreLayout;

    private View headerView;
    TextView tvTotalEarnings;

    private User user;
    private int page = 0;
    private ArrayList<Rebate> datas = new ArrayList<>();
    private RebateAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_earning_details);
        ButterKnife.bind(this);
        user = getApplicationContext().getUser();
        super.onCreate(savedInstanceState);
        init();
    }

    private void init() {
        headerView = LayoutInflater.from(mContext).inflate(R.layout.header_earning_details, null);
        tvTotalEarnings = (TextView) headerView.findViewById(R.id.tv_total_earnings);
        tvTotalEarnings.setText(mIntent.getStringExtra("total"));
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addHeaderView(headerView);
        
        getNetWorker().getRebateList(user.getToken(), page + "");
    }

    @Override
    protected void setListener() {
        refreshLoadmoreLayout.setOnStartListener(new XtomRefreshLoadmoreLayout.OnStartListener() {
            @Override
            public void onStartRefresh(XtomRefreshLoadmoreLayout xtomRefreshLoadmoreLayout) {
                page = 0;
                getNetWorker().getRebateList(user.getToken(), page + "");
            }

            @Override
            public void onStartLoadmore(XtomRefreshLoadmoreLayout xtomRefreshLoadmoreLayout) {
                page++;
                getNetWorker().getRebateList(user.getToken(), page + "");
            }
        });
    }

    @OnClick(R.id.title_left_btn)
    public void onClick() {
        finish();
    }

    @Override
    protected void callBeforeDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case REBAT_LIST:
                showProgressDialog("请稍候");
                break;
            default:
                break;
        }
    }

    @Override
    protected void callAfterDataBack(HemaNetTask netTask) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case REBAT_LIST:
                cancelProgressDialog();
                break;
        }
    }

    @Override
    protected void callBackForServerSuccess(HemaNetTask netTask,
                                            HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case REBAT_LIST:
                @SuppressWarnings("unchecked")
                HemaPageArrayResult<Rebate> bResult = (HemaPageArrayResult<Rebate>) baseResult;
                if (bResult == null || bResult.getObjects() == null)
                    return;
                if (page == 0) {// 刷新
                    refreshLoadmoreLayout.refreshSuccess();
                    datas.clear();
                    this.datas.addAll(bResult.getObjects());
                } else {
                    refreshLoadmoreLayout.loadmoreSuccess();
                    if (bResult.getObjects().size() > 0) {
                        this.datas.addAll(bResult.getObjects());
                    } else {
                        refreshLoadmoreLayout.setLoadmoreable(false);
                        XtomToastUtil.showShortToast(mContext, "已经到最后啦");
                    }
                }
                refreshData();
                break;
        }
    }

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case REBAT_LIST:
                if (page == 0) {// 刷新
                    refreshLoadmoreLayout.refreshFailed();
                }else {
                    refreshLoadmoreLayout.loadmoreFailed();
                }
                XtomToastUtil.showShortToast(mContext, "获取信息失败，"+baseResult.getMsg());
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case REBAT_LIST:
                if (page == 0) {// 刷新
                    refreshLoadmoreLayout.refreshFailed();
                }else {
                    refreshLoadmoreLayout.loadmoreFailed();
                }
                XtomToastUtil.showShortToast(mContext, "获取信息失败，请稍后再试");
                break;
        }
    }

    private void refreshData() {
        if (adapter == null) {
            adapter = new RebateAdapter(mContext, datas);
            recyclerView.setAdapter(adapter);
        }else
            adapter.notifyDataSetChanged();
    }
}
